const { Router } = require('express');
const md5 = require('md5');
const JWT = require('jsonwebtoken');
const User = require('../model/User');
const myOrder = require('../model/myOrder');
const router = new Router();

//请求头像和username
router.get('/minehome', async (req, res) => {
	const userId = req.userInfo._id
	// let { _id } = req.query;
	//根据用户id查找数据库

	let result = await User.find({ _id:userId });
	console.log(result);
	let userinfo = {
		_id: result[0]._id,
		avatar: result[0].avatar,
		username: result[0].username,
	};
	// //返回数据
	res.status(200).json({ message: '请求数据成功', userinfo });
});


// 请求个人资料接口 //http://localhost:2233/api/mine/userinfo
router.get('/userinfo', async (req, res) => {
	// let { _id } = req.query;
	//根据用户id查找数据库
	const _id = req.userInfo._id
	let result = await User.find({ _id });
	let userinfo = {
		_id: result[0]._id,
		avatar: result[0].avatar,
		username: result[0].username,
		gender: result[0].gender,
		age: result[0].age,
		hometown: result[0].hometown,
		industry: result[0].industry,
		hobby: result[0].hobby,
	};
	// //返回数据
	res.status(200).json({ message: '请求数据成功', userinfo });
});


// 修改个人资料接口 //http://localhost:2233/api/mine/userinfo/midify
router.post('/userinfo/modify', async (req, res) => {
	//修改需要token
	let data = req.body;
	const { _id, token, info } = data;
	// console.log(_id, token, info);
	//根据用户id查找数据库原本数据,然后将修改的数据进行写入
	let result = await User.updateMany({ _id }, { ...info });
	//返回数据
	res.status(200).json({ message: '修改成功' });
});


// 修改密码接口 //http://localhost:2233/api/mine/midifypsw
router.post('/modifypsw', async (req, res) => {
	//修改需要token
	let { _id, pswData } = req.body;
	// console.log(_id, pswData);
	//1.根据用户id查找数据库原本数据
	let result = await User.findOne({ _id });
	//2.对比旧密码
	const oldpsw = md5(pswData.password);
	// console.log(pswData.password, result.password, oldpsw);
	if (result.password != oldpsw) {
		res.status(400).json({ message: '原密码不符,请重新输入' });
	} else {
		//修改
		await User.updateOne({ _id }, { password: md5(pswData.newPassword) });
		//返回数据
		res.status(200).json({ message: '修改成功' });
	}
});


//请求订单数据  //http://localhost:2233/api/mine/myorder
router.get('/myorder', async (req, res) => {
	const userId = req.userInfo._id
	let result
	// console.log(userId);
	const{ orederstate } = req.query
	// console.log(orederstate);
	//1.根据用户id查找数据库原本数据
	if(orederstate == 4){
		result = await myOrder.find({ userId }).populate('hotelId').populate('userId');
	}else{
		result = await myOrder.find({ userId,orederstate}).populate('hotelId').populate('userId');
	}

	
	// console.log(result);
	res.status(200).json({ message: '订单获取成功', orderlist: result });
});


// 添加订单接口
router.post('/createmyorder', async (req, res) => {
	const userId = req.userInfo._id
	const{hotelId,totalPrice,nike_name,tel,startdate,enddate,datelength,orederstate,peopleNum,orderTime} = req.body
	//1.根据用户id查找数据库原本数据

	let result = await myOrder.create({ userId,hotelId,totalPrice,nike_name,tel,startdate,enddate,datelength,orederstate,peopleNum,orderTime });
	// console.log(result);

	res.status(200).json({ message: '订单添加成功' , orderId:result._id});
});


// 修改订单状态接口
router.post('/changorderstate', async (req, res) => {
	const userId = req.userInfo._id
	const{ orderId, orederstate} = req.body

	let result = await myOrder.findOneAndUpdate(
		{ userId, _id:orderId},
		{ orederstate }
	  );

	res.status(200).json({ message: '订单状态修改成功'});
});

//请求当前订单详情 
router.get('/orderdetail', async (req, res) => {
	const userId = req.userInfo._id
	const{ orderId } = req.query
	//1.根据订单id查找数据库原本数据
	result = await myOrder.find({ userId,_id:orderId}).populate('hotelId').populate('userId');

	console.log(result);
	res.status(200).json({ message: '订单获取成功', orderDetailData: result });
});

module.exports = router;
